package com.carrey.cms_1810.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.carrey.carrey_utils.util.DateUtil;
import com.carrey.cms_1810.entity.Article;
import com.carrey.cms_1810.entity.Category;
import com.carrey.cms_1810.entity.Channel;
import com.carrey.cms_1810.entity.Comments;
import com.carrey.cms_1810.entity.Link;
import com.carrey.cms_1810.entity.Slide;
import com.carrey.cms_1810.entity.User;
import com.carrey.cms_1810.service.ArticleService;
import com.carrey.cms_1810.service.CategoryService;
import com.carrey.cms_1810.service.ChannelService;
import com.carrey.cms_1810.service.SlideService;
import com.github.pagehelper.PageInfo;

@Controller
public class IndexController {

	@Autowired
	private ArticleService articleService;

	@Autowired
	private CategoryService categoryService;

	@Autowired
	private ChannelService channelService;

	@Autowired
	private SlideService slideService;

	/**
	 * 首页数据展示接口
	 * 
	 * @param pageNum
	 * @param pageSize
	 * @param model
	 * @param article
	 * @return
	 */
	@RequestMapping({ "/", "", "index" })
	public String index(@RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "5") Integer pageSize, Model model, Article article) {

		// 1.查询栏目
		List<Channel> channels = channelService.getChannels();
		Integer channelId = article.getChannelId();
		if (channelId != null) {
			// 1.2查询栏目分类
			List<Category> categories = categoryService.getCategorysByChannelId(channelId);
			// 1.3将数据封装到model
			model.addAttribute("categories", categories);
		} else {
			// 默认查询热点文章
			article.setHot(1);
		}
		// 3.查询轮播图
		List<Slide> slides = slideService.getSlides();

		// 4.查询文章
		article.setStatus(1);
		PageInfo<Article> info = articleService.getArticles(pageNum, pageSize, article);

		// 4.1 24小时热文
		Article a24 = new Article();
		a24.setCreated(DateUtil.resetDate(-1)); // 设置时间，设置为1天前
		a24.setStatus(1);// 审核通过

		PageInfo<Article> info24Hot = articleService.getArticles(1, 5, a24);
		model.addAttribute("info24Hot", info24Hot);

		// 4.2 点击量排行
		Article ahit = new Article();
		ahit.setStatus(1);// 审核通过
		PageInfo<Article> infoHits = articleService.listByHits(1, 5, ahit);
		model.addAttribute("infoHits", infoHits);

		// 5.友情链接
		List<Link> links = articleService.getLinks();
		model.addAttribute("links", links);

		// 6..数据封装到model
		model.addAttribute("channels", channels);
		model.addAttribute("slides", slides);
		model.addAttribute("info", info);
		return "/index/index";
	}

	/**
	 * 文章详情查询
	 * 
	 * @param articleId
	 * @param m
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("detail")
	public String detail(Integer articleId, Model m, @RequestParam(defaultValue = "1") Integer pageNum,
			@RequestParam(defaultValue = "3") Integer pageSize) {

		// 根据id查询文章数据
		Article article = articleService.getDetailById(articleId);

		// 查询文章的评论
		PageInfo<Comments> info = articleService.getCommentsByArticleId(articleId, pageNum, pageSize);

		// 将文章数据返回为jsp
		m.addAttribute("article", article);
		m.addAttribute("info", info);
		return "index/detail";
	}

	/**
	 * 添加文章评论
	 * 
	 * @param comments
	 * @param session
	 * @return
	 */
	@RequestMapping("addComments")
	@ResponseBody
	public boolean addComments(Comments comments, HttpSession session) {

		// 获取当前登录用户
		User user = (User) session.getAttribute("LOGIN_USER");

		// 设置用户id和当前时间
		comments.setUserId(user.getId());
		comments.setCreated(new Date());

		// 将评论添加到数据库
		boolean res1 = articleService.addComments(comments);

		// 根据id查询文章数据
		Article article = articleService.getDetailById(comments.getArticleId());

		// 修改评论数据
		if (article.getComments() == null) {
			article.setComments(1); // 首次评论
		} else {
			article.setComments(article.getComments() + 1);
		}
		// 修改数据库文章的评论数量
		boolean res2 = articleService.update(article);
		return res1 && res2;
	}

	/**
	 * addHit
	 * 
	 * @param comments
	 * @param session
	 * @return
	 */
	@RequestMapping("addHit")
	@ResponseBody
	public boolean addHit(Integer id) {

		// 根据id查询文章数据
		Article article = articleService.getDetailById(id);
		if (article == null) {
			return false;
		}
		// 修改点击量数据
		if (article.getHits() == null) {
			article.setHits(1); // 首次查看
		} else {
			article.setHits(article.getHits() + 1);
		}
		// 修改数据库文章的点击量数量
		return  articleService.update(article);
	}

}
